package com.example.filter;

import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Order(1)
@Component
@WebFilter(filterName = "pathFilter", urlPatterns = {"/*"})
public class PathFilter implements Filter {
    @Override
    public void destroy() {

    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse resp,
                         FilterChain filterChain) throws IOException, ServletException {
        HttpServletResponse response =(HttpServletResponse) resp;
        HttpServletRequest request =(HttpServletRequest) req;

        String requestURI = request.getRequestURI();

        if (requestURI.contains("/platform")) {
            if (!requestURI.equals("/platform/index.html") && !requestURI.endsWith(".css") && !requestURI.endsWith(".js") && !requestURI.endsWith(".png")) {
                RequestDispatcher dispatcher = request.getRequestDispatcher("/platform/index.html");
                dispatcher.forward(request, response);//执行转发
                //response.sendRedirect("/platform/index.html");
            } else {
                filterChain.doFilter(req, resp);
            }
        } else {
            filterChain.doFilter(req, resp);
        }
    }

    @Override
    public void init(FilterConfig cfg) throws ServletException {

    }
}
